package com.lc.sms.handle;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.lc.sms.common.constant.SmsConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @description: aliyun短信服务，单条短信+批量短信
 * @author: LChuang
 * @date: 2023/9/22 14:02
 */

public interface SmsHandle {

    Logger log = LoggerFactory.getLogger(SmsHandle.class);

    default void sendMsg(String code, String phone, JSONObject jsonObject){
        // TODO 根据code从字典获取模板
        String templateCode = SmsConstant.SMS_TEMPLATE;
        // 获取客户端
        DefaultAcsClient client = getClient();
        SendSmsRequest request = new SendSmsRequest();
        request.setMethod(MethodType.POST);
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", SmsConstant.SIGN_NAME);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", jsonObject.toJSONString());
        SendSmsResponse acsResponse;
        try {
            acsResponse = client.getAcsResponse(request);
            if("OK".equalsIgnoreCase(acsResponse.getMessage())) {
                log.info("阿里云短信服务调用成功！模板：{}，发送人：{}", templateCode, phone);
            }else {
                log.info("阿里云短信服务调用失败！模板：{}，发送人：{}，失败原因：{}", templateCode, phone, acsResponse.getMessage());
            }
        } catch (ClientException e) {
            log.error("阿里云短信服务调用异常！模板：{}，发送人：{}，异常信息：{}", templateCode, phone, e.getErrMsg());
        }
    }

    default void sendBatchMsg(String code, List<String> phone, List<JSONObject> jsonObjectList) {
        // TODO 根据code从字典获取模板
        String templateCode = SmsConstant.SMS_TEMPLATE;
        // 获取客户端
        DefaultAcsClient client = getClient();
        SendSmsRequest request = new SendSmsRequest();
        request.setMethod(MethodType.POST);
        request.putQueryParameter("PhoneNumberJson", JSONArray.toJSONString(phone));
        request.putQueryParameter("SignNameJson", SmsConstant.SIGN_NAME);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParamJson", JSONArray.toJSONString(jsonObjectList));
        SendSmsResponse acsResponse;
        try {
            acsResponse = client.getAcsResponse(request);
            if("OK".equalsIgnoreCase(acsResponse.getMessage())) {
                log.info("阿里云短信服务调用成功！模板：{}，发送人：{}", templateCode, phone);
            }else {
                log.info("阿里云短信服务调用失败！模板：{}，发送人：{}，失败原因：{}", templateCode, phone, acsResponse.getMessage());
            }
        } catch (ClientException e) {
            log.error("阿里云短信服务调用异常！模板：{}，发送人：{}，异常信息：{}", templateCode, phone, e.getErrMsg());
        }
    }

    default DefaultAcsClient getClient(){
        // 调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");
        DefaultProfile profile = DefaultProfile.getProfile(SmsConstant.REGION_ID, SmsConstant.ACCESS_KEY_ID, SmsConstant.ACCESS_KEY_SECRET);
        DefaultProfile.addEndpoint(SmsConstant.REGION_ID, SmsConstant.PRODUCT, SmsConstant.DOMAIN);
        return new DefaultAcsClient(profile);
    }
}
